//
//  PickerBackgroundView.m
//  Qihuo
//
//  Created by Vencoo on 13-11-13.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//
#import "Appinfomation.h"
#import "PickerBackgroundView.h"
#import "NSDate+Calendar.h"
@implementation PickerBackgroundView
{
    UIImageView * addImageView;
    UIImage* backgroundimage;
    UIImage *sureImage;
    UIPickerView *Datepicker;
    UIPickerView *DateAndTimePick;
    NSString *_year;
    NSString *_month;
    NSString *_day;
    NSString *_year1;
    NSString *_month1;
    NSString *_day1;
    NSString *_hour;
    NSString *_minute;
    NSString *_second;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        sureImage = [UIImage imageNamed:@"确定按钮"];
        _timeStr = [NSString string];
        _timeDateStr = [NSString string];
        UIImageView* greyView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,1024,768)];
        greyView.userInteractionEnabled = YES;
        greyView.image = [UIImage imageNamed:@"黑底1"];
        [self addSubview:greyView];
        
        backgroundimage = [UIImage imageNamed:@"选择日期的底123"];
        addImageView = [[UIImageView alloc]initWithImage:backgroundimage];
        addImageView.frame = CGRectMake((greyView.frame.size.width - backgroundimage.size.width)/2, (greyView.frame.size.height - backgroundimage.size.height)/2, backgroundimage.size.width, backgroundimage.size.height);
        addImageView.userInteractionEnabled = YES;
        [greyView addSubview:addImageView];
        //========================================
        UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake((backgroundimage.size.width - sureImage.size.width*2)/3, 20, backgroundimage.size.width - ((backgroundimage.size.width - sureImage.size.width*2)/3)*2, backgroundimage.size.height - 40)];
        //========================================
        UIImage *cancelImage = [UIImage imageNamed:@"取消按钮"];
        for (int i = 0; i<2; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake((backgroundimage.size.width - sureImage.size.width*2)/3*(i+1) + sureImage.size.width*i, picker.frame.origin.y + picker.frame.size.height + 40, sureImage.size.width, cancelImage.size.height);
            [btn addTarget:self action:@selector(sureOrCancel:picker:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i;
            if (btn.tag == 0) {
                [btn setImage:sureImage forState:UIControlStateNormal];
            }else{
                [btn setImage:cancelImage forState:UIControlStateNormal];
            }
            [addImageView addSubview:btn];
        }
        
    }
    return self;
}
- (void)hidden
{
    self.hidden = YES;
    self.delegate.navigationController.navigationBar.hidden = NO;
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}
- (void)canDate
{
    Datepicker = [[UIPickerView alloc]initWithFrame:CGRectMake((backgroundimage.size.width - sureImage.size.width*2)/3, 20, backgroundimage.size.width - ((backgroundimage.size.width - sureImage.size.width*2)/3)*2, backgroundimage.size.height - 40)];
    Datepicker.delegate = self;
    Datepicker.dataSource = self;
    Datepicker.showsSelectionIndicator = YES;
    [addImageView addSubview:Datepicker];
    
    NSDate *date = [NSDate date];
    [Datepicker selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.year] IndexOfArray:[NSDate year]] inComponent:0 animated:YES];
    [Datepicker selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.month] IndexOfArray:[NSDate month]] inComponent:1 animated:YES];
    [Datepicker selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.day] IndexOfArray:[NSDate day]] inComponent:2 animated:YES];
    
}
- (void)canDateAndTime
{
    DateAndTimePick = [[UIPickerView alloc]initWithFrame:CGRectMake((backgroundimage.size.width - sureImage.size.width*2)/3 - 50, 20, backgroundimage.size.width - ((backgroundimage.size.width - sureImage.size.width*2)/3)*2 + 100, backgroundimage.size.height - 40)];
    DateAndTimePick.delegate = self;
    DateAndTimePick.dataSource = self;
    DateAndTimePick.showsSelectionIndicator = YES;
    [addImageView addSubview:DateAndTimePick];
    
    NSDate *date = [NSDate date];
    [DateAndTimePick selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.year] IndexOfArray:[NSDate year]] inComponent:0 animated:YES];
    [DateAndTimePick selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.month] IndexOfArray:[NSDate month]] inComponent:1 animated:YES];
    [DateAndTimePick selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.day] IndexOfArray:[NSDate day]] inComponent:2 animated:YES];
    [DateAndTimePick selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.hour] IndexOfArray:[NSDate hour]] inComponent:3 animated:YES];
    [DateAndTimePick selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.minute] IndexOfArray:[NSDate minute]] inComponent:4 animated:YES];
    [DateAndTimePick selectRow:[NSDate object:[NSString stringWithFormat:@"%d",date.second] IndexOfArray:[NSDate second]] inComponent:5 animated:YES];
    
}
- (void)sureOrCancel:(UIButton*)btn picker:(UIPickerView*)pickerView
{
    [self.butonDelegate sureOrCancelDelegate:btn tag:self.tag];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (pickerView == DateAndTimePick) {
        return 6;
    }else{
        return 3;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == Datepicker) {
        switch (component) {
            case 0:
                return [NSDate year].count;
                break;
            case 1:
                return [NSDate month].count;
            case 2:
                return [NSDate day].count;
            default:
                break;
        }
        
    }else if (pickerView == DateAndTimePick)
    {
        switch (component) {
            case 0:
                return [NSDate year].count;
                break;
            case 1:
                return [NSDate month].count;
            case 2:
                return [NSDate day].count;
            case 3:
                return [NSDate hour].count;
            case 4:
                return [NSDate minute].count;
            case 5:
                return [NSDate second].count;
            default:
                break;
        }
        
    }
    return 0;
}
#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([pickerView isEqual:Datepicker]) {
        _year = [[NSDate year]objectAtIndex:[pickerView selectedRowInComponent:0]];
        _month = [[NSDate month]objectAtIndex:[pickerView selectedRowInComponent:1]];
        _day = [[NSDate day]objectAtIndex:[pickerView selectedRowInComponent:2]];
        NSString *dateStr = [NSString stringWithFormat:@"%04d年%02d月%02d日",[_year intValue],[_month intValue],[_day intValue]];
        _timeStr = dateStr;
    }
    else if ([pickerView isEqual:DateAndTimePick])
    {
        _year1 = [[NSDate year]objectAtIndex:[pickerView selectedRowInComponent:0]];
        _month1 = [[NSDate month]objectAtIndex:[pickerView selectedRowInComponent:1]];
        _day1 = [[NSDate day]objectAtIndex:[pickerView selectedRowInComponent:2]];
        _hour = [[NSDate hour]objectAtIndex:[pickerView selectedRowInComponent:3]];
        _minute = [[NSDate minute]objectAtIndex:[pickerView selectedRowInComponent:4]];
        _second = [[NSDate second]objectAtIndex:[pickerView selectedRowInComponent:5]];
        NSString *timeDateStr = [NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d",[_year1 intValue],[_month1 intValue],[_day1 intValue],[_hour intValue],[_minute intValue],[_second intValue]];
        _timeDateStr = timeDateStr;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView == Datepicker) {
        switch (component)
        {
            case 0:
            {
                return [[NSDate year]objectAtIndex:row];
            }
                break;
            case 1:
            {
                return [[NSDate month]objectAtIndex:row];
            }
            default:
            {
                return [[NSDate day]objectAtIndex:row];
            }
                break;
        }
        
    }else if (pickerView == DateAndTimePick)
    {
        switch (component)
        {
            case 0:
            {
                return [[NSDate year]objectAtIndex:row];
            }
                break;
            case 1:
            {
                return [[NSDate month]objectAtIndex:row];
            }
            case 2:
            {
                return [[NSDate day]objectAtIndex:row];
            }
                
            case 3:
            {
                return [[NSDate hour]objectAtIndex:row];
            }
            case 4:
            {
                return [[NSDate minute]objectAtIndex:row];
            }
            case 5:
            {
                return [[NSDate second]objectAtIndex:row];
            }
                
            default:
            {
            }
                break;
        }
        
    }
    return nil;
}

@end
